package com.xiaotou.struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opensymphony.xwork2.ActionSupport;
import com.xiaotou.db.pojo.WbUser;
import com.xiaotou.service.UserService;
import com.xiaotou.utils.AuthUtils;
import com.xiaotou.weibo.OauthUtils;
import com.xiaotou.weibo.WeiboFactory;
import com.xiaotou.weibo.appData.AbstractAppOauthData;

public class LoginAction extends ActionSupport implements ServletRequestAware,
		ServletResponseAware {

	private static final long serialVersionUID = -5834960362102315072L;

	private static Logger logger = LoggerFactory.getLogger(LoginAction.class);

	private OauthUtils oauthUtils;

	private UserService userService;

	private HttpServletRequest request;

	private HttpServletResponse response;

	private String wbType;

	private String oauth_token;

	private String oauth_verifier;

	private boolean success = false;

	public String getWbType() {
		return wbType;
	}

	public void setWbType(String wbType) {
		this.wbType = wbType;
	}

	@Action(value = "/login", results = { @Result(name = "success", location = "/WEB-INF/jsp/login.jsp") })
	public String login() {
		final Long userId = AuthUtils.getUserIdFromCookie();
		if (userId != null) {
			final WbUser wbUser = userService.getWbUser(userId);
			if (wbUser != null) {
				AuthUtils.setLoginUser(wbUser);
				return "main";
			} else {
				return SUCCESS;
			}
		} else {
			return SUCCESS;
		}
	}

	@Action(value = "/getOauthAuthriseUrl", results = { @Result(name = "success", location = "/WEB-INF/jsp/login.jsp") })
	public String getOauthAuthriseUrl() {
		String authrizeURL = oauthUtils.getAuthrizeURL(
				WeiboFactory.getAppOauthData(wbType), request);
		response.setHeader("authriseUrl", authrizeURL);
		return SUCCESS;
	}

	@Action(value = "/wbcallback", results = { @Result(name = "success", location = "preys", type = "redirectAction") })
	public String wbcallback() {
		String tokenSecret = AuthUtils.getTokenSecrct();
		if (StringUtils.isNotBlank(oauth_token)
				&& StringUtils.isNotBlank(tokenSecret)
				&& StringUtils.isNotBlank(oauth_verifier)) {
			AbstractAppOauthData appData = WeiboFactory.getAppOauthData(wbType);
			appData.setToken(oauth_token);
			appData.setTokenSecret(tokenSecret);
			appData.setVerifier(oauth_verifier);
			try {
				WbUser wbUser = oauthUtils
						.storeAccessTokenAndAccessTokenSecret(appData);
				AuthUtils.setLoginUser(wbUser);
				AuthUtils.putUserIdToCookie(wbUser.getId());
				success = true;
			} catch (Exception e) {
				logger.error(
						"error when store accessToken and accessTokenSecret", e);
			}
		}
		return SUCCESS;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	/**
	 * @param oauthUtils
	 *            the oauthUtils to set
	 */
	public void setOauthUtils(OauthUtils oauthUtils) {
		this.oauthUtils = oauthUtils;
	}

	public void setOauth_token(String oauth_token) {
		this.oauth_token = oauth_token;
	}

	public void setOauth_verifier(String oauth_verifier) {
		this.oauth_verifier = oauth_verifier;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

}
